package web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import vo.User;
import vo.Warn;
import business.TopicService;
import business.UserService;
import business.WarnService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("unchecked")
public class WarnAction implements Preparable {
	private WarnService service;
	private UserService userService;
	private TopicService topicService;
	private List<Warn> warns;
	private ArrayList<String> delIds = new ArrayList<String>();
	private String name;
	private long userId;
	private long topicId;


	private User curUser;

	// 分页的变量
	private int curPage;
	private int totalPage;
	private int pageSize = 5;

	public String deleteWarn() {
		System.out.println("delete warn:");
		for (int i = 0; i < this.delIds.size(); i++) {
			System.out.println("deleted ids：" + delIds.get(i));
			long id = Long.parseLong(delIds.get(i));
			service.delete(id);

		}
		return "warnDeleted";
	}

	// 查看所有人的预警
	public String listWarn() {
		this.warns = service.getAllWarns();


		System.out.println("curPage:" + curPage);
		int totalCount = (int) service.getTotalCount();
		System.out.println("totalCount:" + totalCount);
		if (totalCount % pageSize == 0)
			totalPage = totalCount / pageSize;
		else
			totalPage = totalCount / pageSize + 1;
		System.out.println("totalCount / pageSize:" + totalCount / pageSize);

		if (curPage == 0)
			curPage = 1;
		this.warns = service.getAllWarns((curPage - 1) * pageSize, pageSize);
		System.out.println("warns count:" + this.warns.size());

		return Action.SUCCESS;
	}

	public String addWarn() {
		service.addWarn("hello xxx");
		return listWarn();
	}

	public String saveWarn() {
		System.out.println("form save warn:" + name + "," + userId);
		Warn warn = new Warn();
		warn.setName(name);
		warn.setUser(userService.getById(userId));
		warn.setTopic(topicService.getById(topicId));
		service.addWarn(warn);
		return "saveWarn";
	}

	public List<Warn> getWarns() {
		return warns;
	}

	public void prepare() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		String username = (String) session.get("username");
		curUser = userService.getByLoginName(username);
		System.out.println("curUser.getLoginName():" + curUser.getLoginName());
	}

	public void setWarnService(WarnService service) {
		this.service = service;
	}

	public void setService(WarnService service) {
		this.service = service;
	}

	public void setWarns(List<Warn> warns) {
		this.warns = warns;
	}

	public ArrayList<String> getDelIds() {
		return delIds;
	}

	public void setDelIds(ArrayList<String> delIds) {
		this.delIds = delIds;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getUserId() {
		return userId;
	}

	public void setUserId(long userId) {
		this.userId = userId;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}

	public long getTopicId() {
		return topicId;
	}

	public void setTopicId(long topicId) {
		this.topicId = topicId;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public User getCurUser() {
		return curUser;
	}

	public void setCurUser(User curUser) {
		this.curUser = curUser;
	}

}